from tkinter import * 
import tkinter as tk  # as 表示 别名

win = Tk()
win.geometry('400x120')
win.title('单选按钮和复选框示例')

# 显示选择状态的标签
txt=StringVar()
txt.set("请选择")
lab=Label(win,textvariable=txt,relief='ridge',width=30,font=('华文新魏','16')) 

# 复选框
chVarDis = tk.IntVar()   # 用来获取复选框是否被勾选，通过chVarDis.get()来获取其的状态,其状态值为int类型 勾选为1  未勾选为0
check1 = tk.Checkbutton(win, text="C语言", variable=chVarDis,statue='disabled')
#  text为该复选框后面显示的名称, variable将该复选框的状态赋值给一个变量，当state='disabled'时，该复选框为灰色，不能点的状态
check1.select()     # 该复选框是否勾选,select为勾选, deselect为不勾选

chvarUn = tk.IntVar()
check2 = tk.Checkbutton(win, text="Java", variable=chvarUn)
check2.deselect()

chvarEn = tk.IntVar()
check3 = tk.Checkbutton(win, text="Python", variable=chvarEn)
check3.select()

# 单选按钮
chk = ["鲜花", "鼓掌", "鸡蛋"]# 定义几个选项的全局变量

# 单选按钮回调函数,就是当单选按钮被点击会执行该函数
def radCall():
    radSel = radVar.get()
    if radSel == 0:
        txt.set(chk[0])
    elif radSel == 1:
        txt.set(chk[1])
    elif radSel == 2:
        txt.set(chk[2])
    print(radVar.get())

radVar = tk.IntVar()  # 通过tk.IntVar() 获取单选按钮value参数对应的值
for col in range(3):
  curRad = tk.Radiobutton(win, text=chk[col], variable=radVar, value=col, command=radCall)    # 当该单选按钮被点击时，会触发参数command对应的函数
  curRad.grid(column=col, row=5, sticky=tk.W)     # 参数sticky对应的值参考复选框的解释

#布局设置
lab.grid(row=0,column=0,columnspan=3)
check1.grid(column=0, row=4, sticky=tk.W) 
check2.grid(column=1, row=4, sticky=tk.W)
check3.grid(column=2, row=4, sticky=tk.W)
#其中：sticky=tk.W 为设置行列对齐，N：北/上对齐  S：南/下对齐  W：西/左对齐  E：东/右对齐

win.mainloop()
